package com.survey.service.impl;

import com.github.pagehelper.PageHelper;
import com.survey.dao.mapper.LinkMapper;
import com.survey.pojo.data.LinkDO;
import com.survey.service.LinkService;
import com.survey.utils.PageBean;
import com.survey.utils.Result;
import com.survey.pojo.vo.LinkVO;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/** 
 * <br/>
 * Created by survey on 2018/08/03
 */
@Service("linkService")
public class LinkServiceImpl implements LinkService {

	private final static Logger LOG = LoggerFactory.getLogger(LinkServiceImpl.class);

	@Resource
	private LinkMapper linkMapper;

	@Override
	public Result addLink(LinkVO linkVO) throws Exception {
		Integer row = linkMapper.addLink(linkVO);
		if(row>0){
			return new Result(Result.CODE_SUCCESS,"添加成功");
		}
		return new Result(Result.CODE_FAILURE,"添加失败");
	}

	@Override
	public Result updateLink(LinkVO linkVO) throws Exception {
		Integer row = linkMapper.updateLink(linkVO);
		if(row>0){
			return new Result(Result.CODE_SUCCESS,"修改成功");
		}
		return new Result(Result.CODE_SUCCESS,"修改失败");
	}
	
	@Override
	public LinkDO findDetailLink(LinkVO linkVO) throws Exception{
        return linkMapper.findDetailLink(linkVO);
    }

	@Override
	public List<LinkDO> listLink(LinkVO linkVO) throws Exception{
		return linkMapper.listLink(linkVO);
	}

	@Override
	public List<LinkDO> listLink() throws Exception {
		List<LinkDO> linkDOS = linkMapper.listLink();
		return linkDOS;
	}

	@Override
	public Result listLinkPage(LinkVO linkVO) throws Exception{
		return null;
	}
	
	@Override
	public Result countLink(LinkVO linkVO) throws Exception{
		return null;
	}
	
	@Override
	public Result deleteLink(LinkVO linkVO) throws Exception{
		Integer row = linkMapper.deleteLink(linkVO);
		if(row>0){
			return new Result(Result.CODE_SUCCESS,"删除成功");
		}
		return new Result(Result.CODE_FAILURE,"删除失败");
	}

	@Override
	public PageBean<LinkVO> listLinkByPage(LinkVO linkVO) throws InvocationTargetException, IllegalAccessException {
		Integer count = linkMapper.countLink(linkVO);
		//越界异常
		PageHelper.startPage(linkVO.getPageCode(),linkVO.getPageSize());
		List<LinkDO> linkDOS = linkMapper.listLink();
		ArrayList<LinkVO> linkVOS=new ArrayList<>();
		for (LinkDO linkDO : linkDOS) {
			LinkVO vo=new LinkVO();
			BeanUtils.copyProperties(vo,linkDO);
			linkVOS.add(vo);
		}

		PageBean<LinkVO> pb=new PageBean(linkVOS,linkVO.getPageSize()
				, linkVO.getPageCode(), count, linkVO.getSize());
		return pb;

	}

}